<template>
  <el-dialog
    :title="!dataForm.id ? '无' : '详细'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="200px">
      <el-form-item label="学员名字" prop="studentname">
        <el-input v-model="dataForm.studentname" placeholder="学员名字" readOnly="true"></el-input>
      </el-form-item>
      <el-form-item label="会员卡" prop="cardtype" >
        <el-select v-model="dataForm.cardtype" placeholder= "会员卡类型"  @change = 'getMemberCardTime()' disabled="disabled">
          <el-option v-for="(item, index) in payeeoption"
                     :label="item.cardName"
                     :key="item.cardName"
                     :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="课程" prop="chooseCourse" >
        <el-select v-model="dataForm.chooseCourse" placeholder= "课程" @change="getOneCourseTimes()" disabled="disabled">
          <el-option v-for="(item, index) in chooseCourseList"
                     :label="item.name"
                     :key="item.name"
                     :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="课程时间">
      <el-select v-model="dataForm.chooseCoursetime" placeholder="请选择课程时间" >
      <el-option v-for="item in chooseCoursetimes" :key="item.id" :label="item.courseTimes" :value="item.id">
      </el-option>
      </el-select>
      </el-form-item>
      <el-form-item label="签到时间" prop="signTime">
        <el-date-picker v-model="dataForm.signTime" type="datetime"  placeholder="签到时间" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" ></el-date-picker>
      </el-form-item>
      <el-form-item label="签到老师">
        <el-select v-model="dataForm.chooseTeacher" placeholder="请选择签到老师" >
          <el-option v-for="item in chooseTeachers" :key="item.id" :label="item.teacherName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">返回</el-button>
      <el-button type="primary" @click="sign()">签到</el-button>
    </span>
  </el-dialog>
</template>

<script>
  /* eslint-disable no-unused-vars */

  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          shopid: '',
          studentname: '',
          studentage: '',
          studentmather: '',
          matherphone: '',
          studentfather: '',
          fatherphone: '',
          other: '',
          otherphone: '',
          male: '',
          studentpic: '',
          residualintegral: '',
          cardtype: {'coursetimes': '', 'id': '', 'cardname': '', 'surplus': null, 'teachername': null},
          cardstatus: '',
          cardnum: '',
          giftequipment: '',
          residualclasshour: '',
          duetime: '',
          payment: '',
          remark: '',
          createtime: '',
          updatetime: '',
          cardtypeInfo: '',
          chooseCourse: '',
          chooseCoursetime: [],
          chooseTeacher: '',
          signTime: ''
        },
        payeeoption: '',
        chooseCourseList: '',
        chooseCoursetimes: '',
        chooseTeachers: '',
        shopids: '',
        signStud: [],
        dataRule: {
        }
      }
    },
    mounted () {
      this.getMemberCard()
      this.getShopInfo()
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/business/student/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.shopid = data.student.shopid
                this.dataForm.studentname = data.student.studentname
                this.dataForm.studentage = data.student.studentAge
                this.dataForm.studentmather = data.student.studentmather
                this.dataForm.matherphone = data.student.matherphone
                this.dataForm.studentfather = data.student.studentfather
                this.dataForm.fatherphone = data.student.fatherphone
                this.dataForm.other = data.student.other
                this.dataForm.otherphone = data.student.otherphone
                this.dataForm.male = data.student.male
                this.dataForm.studentpic = data.student.studentpic
                this.dataForm.residualintegral = data.student.residualintegral
                this.dataForm.cardtype = data.student.cardtype
                this.dataForm.cardstatus = data.student.cardstatus
                this.dataForm.cardnum = data.student.cardnum
                this.dataForm.giftequipment = data.student.giftequipment
                this.dataForm.residualclasshour = data.student.residualclasshour
                this.dataForm.duetime = data.student.duetime
                this.dataForm.payment = data.student.payment
                this.dataForm.remark = data.student.remark
                this.dataForm.createtime = data.student.createtime
                this.dataForm.updatetime = data.student.updatetime
                this.dataForm.refereename = data.student.refereename
                this.dataForm.address = data.student.address
                this.dataForm.school = data.student.school
                this.dataForm.consultant = data.student.consultant
                this.getOneCourse()
                this.getTeacher()
              }
            })
          } else {
            this.dataForm.chooseCoursetime = []
            this.dataForm.chooseCourse = ''
            this.dataForm.cardtype = ''
          }
        })
      },
      // 查询会员卡状态
      getMemberCard () {
        this.$http({
          url: this.$http.adornUrl('/business/basemembercard/getMemberCard'),
          method: 'get',
          params: ''
        }).then(({data}) => {
          this.payeeoption = data
        })
      },
      // 查询会员卡课时
      getMemberCardTime () {
        let cardId = this.dataForm.cardtype
        let payeeoption = this.payeeoption
        for (var i = 0; i < payeeoption.length; i++) {
          if (cardId === payeeoption[i].id) {
            this.dataForm.residualclasshour = payeeoption[i].coursetimes
          }
        }
      },
      // 根据店铺，查询可选课程
      getCourse () {
        this.$http({
          url: this.$http.adornUrl(`/business/student/getCourse`),
          method: 'get',
          params: {'shopid': this.dataForm.shopid}
        }).then(({data}) => {
          this.chooseCourseList = data.courseEntityList
          this.dataForm.chooseCoursetime = []
          this.dataForm.chooseCourse = ''
        })
      },
      getTeacher () {
        this.$http({
          url: this.$http.adornUrl(`/business/student/getTeacher`),
          method: 'get',
          params: {'shopid': this.dataForm.shopid}
        }).then(({data}) => {
          this.chooseTeachers = data.teacherEntityList
        })
      },
      getCourse2 () {
        this.$http({
          url: this.$http.adornUrl(`/business/student/getCourse`),
          method: 'get',
          params: {'shopid': this.dataForm.shopid}
        }).then(({data}) => {
          this.chooseCourseList = data.courseEntityList
        })
      },
      // 查询学生所选课程
      getOneCourse () {
        this.$http({
          url: this.$http.adornUrl(`/business/student/getOneCourse`),
          method: 'get',
          params: {'id': this.dataForm.id}
        }).then(({data}) => {
          this.dataForm.chooseCourse = data.courseStudentEntity.courseId
          this.getOneCourseTimes2()
          // this.getOneStudentCourseTimes()
          this.getCourse2()
        })
      },
      // 查询学生所选课程的包含的所有课程时间
      getOneCourseTimes () {
        let courseid = this.dataForm.chooseCourse
        this.$http({
          url: this.$http.adornUrl(`/business/course/getCourseTimes`),
          method: 'get',
          params: {'id': courseid}
        }).then(({data}) => {
          this.chooseCoursetimes = data.courseTimes
          this.dataForm.chooseCoursetime = []
        })
      },

      // 查询学生所选课程的包含的所有课程时间
      getOneCourseTimes2 () {
        let courseid = this.dataForm.chooseCourse
        this.$http({
          url: this.$http.adornUrl(`/business/course/getCourseTimes`),
          method: 'get',
          params: {'id': courseid}
        }).then(({data}) => {
          this.chooseCoursetimes = data.courseTimes
        })
      },

      // 查询学生选课时，已选择好的课程时间
      getOneStudentCourseTimes () {
        let courseid = this.dataForm.chooseCourse
        let studentid = this.dataForm.id
        this.$http({
          url: this.$http.adornUrl(`/business/course/getOneStudentCourseTimes`),
          method: 'get',
          params: {'courseId': courseid, 'studentId': studentid}
        }).then(({data}) => {
          this.dataForm.chooseCoursetime = data.list
        })
      },
      // 获取机构下所有店铺信息
      getShopInfo () {
        let trainId = '1'
        this.$http({
          url: this.$http.adornUrl('/business/student/getShopInfo'),
          mothod: 'get',
          params: {'trainId': trainId}
        }).then(({data}) => {
          this.shopids = data.list
        })
      },
      // 后台学生签到
      sign () {
        var id = this.dataForm.id
        var cdId = this.dataForm.chooseCoursetime
        var teacherId = this.dataForm.chooseTeacher
        for (var i = 0; i < this.chooseTeachers.length; i++) {
          if (this.chooseTeachers[i].id === teacherId) {
            var teacherName = this.chooseTeachers[i].teacherName
          }
        }
        if (!cdId) {
          this.$message({
            message: '请选择课程时间进行签到',
            type: 'false',
            duration: 1500
          })
          return
        }
        for (var j = 0; j < this.signStud.length; j++) {
          if (this.signStud[j] === id + cdId + '今日已签到') {
            this.$message({
              message: '今日已签到',
              type: 'false',
              duration: 1500
            })
            return
          }
        }
        this.$http({
          url: this.$http.adornUrl(`/business/student/signInBybackstage`),
          method: 'get',
          params: {
            'studentId': id,
            'cdId': cdId,
            'teacherId': teacherId,
            'teacherName': teacherName,
            'signTime': this.dataForm.signTime
          }
        }).then(({data}) => {
          if (data.code === 0) {
            this.$message({
              message: '签到成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
            this.dataForm.chooseCoursetime = []
            this.signStud.push(id + cdId + '今日已签到')
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/business/student/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'shopid': this.dataForm.shopid,
                'studentname': this.dataForm.studentname,
                'studentage': this.dataForm.studentage,
                'studentmather': this.dataForm.studentmather,
                'matherphone': this.dataForm.matherphone,
                'studentfather': this.dataForm.studentfather,
                'fatherphone': this.dataForm.fatherphone,
                'other': this.dataForm.other,
                'otherphone': this.dataForm.otherphone,
                'male': this.dataForm.male,
                'studentpic': this.dataForm.studentpic,
                'residualintegral': this.dataForm.residualintegral,
                'cardtype': this.dataForm.cardtype,
                'cardstatus': this.dataForm.cardstatus,
                'cardnum': this.dataForm.cardnum,
                'giftequipment': this.dataForm.giftequipment,
                'residualclasshour': this.dataForm.residualclasshour,
                'duetime': this.dataForm.duetime,
                'payment': this.dataForm.payment,
                'remark': this.dataForm.remark,
                'createtime': this.dataForm.createtime,
                'updatetime': this.dataForm.updatetime,
                'courseId': this.dataForm.chooseCourse,
                'coursetimeid': this.dataForm.chooseCoursetime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
